#!/opt/local/bin/perl

$output = "[";
$i = 0;
while (<STDIN>) {
    if ($i == 0) {
      $i = 1;
    } else {
	    $output .= "{" ;
	    @columns=split("\t");
	#    $output .= @columns[0] . "\n";
	    $output .= "'A': {";
	    ($id,$label) = split(" '",@columns[0]);
	    # check for single quote at end of line, lop it off if found
	    $output .= "'id':'$id','label':'$label'";
	    $output .= '},';
	    $output .= "'B': {";
	    ($id,$label) = split(" '",@columns[1]);
	    $output .= "'id':'$id','label':'$label'";
	    $output .= '},';
	    $num_annot_a = @columns[2];
	    $output .= "'num_annots_A':$num_annot_a,";
	    $num_annot_b = @columns[3];
	    $output .= "'num_annots_B':$num_annot_b,";
	    if (@columns[4]) {
	      $simj = @columns[4];
	    } else {
	      $simj = "null";
	    }
	    $output .= "'simJ':$simj,";
	    #$bma_asymj = @columns[5];
	    if (@columns[5]) {
	      $bma_asymj = @columns[5];
	    } else {
	      $bma_asymj = "null";
	    }
	    $output .= "'BMAasymJ':$bma_asymj,";
	     if (@columns[6]) {
	      $bma_symj = @columns[6];
	    } else {
	      $bma_symj = "null";
	    }
	    $output .= "'BMAsymJ':$bma_symj,";
	    $simgic = @columns[7];
	    if (@columns[7]) {
	      $simgic = @columns[7];
	    } else {
	      $simgic = "null";
	    }
	    $output .= "'SimGIC':$simgic,";
	    $bma_asym_terms = @columns[8];
	    # trim the string
	    $bma_asym_terms =~ s/^\s+//;
    	$bma_asym_terms =~ s/\s+$//;
	    
	    $output .= "'BMAasymIC_terms': [";
	    if ($bma_asym_terms) {
	      @asym_terms = split(" \\| ", $bma_asym_terms);
	      foreach $term  (@asym_terms) {
	        #  $output .= "$term\n";
	        ($id,$label) = split(" '",$term);
	        $output .= "{'id':'$id','label':'$label'},";
	      }
	      # remove last comma from string
	      chop($output); 
	      $output .= "],";
	    } else {
	      # remove the [
	      chop($output);
	      $output .= "null, ";
	    }
	    $bma_asymIC_terms = @columns[9];
	    # trim the string
	    $bma_asymIC_terms =~ s/^\s+//;
    	$bma_asymIC_terms =~ s/\s+$//;
	    $output .= "'BMAsymIC_terms': [";
	    if ($bma_asymIC_terms) {
	      @asymIC_terms = split(" \\| ", $bma_asymIC_terms);
	      foreach $term  (@asymIC_terms) {
	        #  $output .= "$term\n";
	        ($id,$label) = split(" '",$term);
	        $output .= "{'id':'$id','label':'$label'},";
	      }
	      # remove last comma from string
	      chop($output); 
	      $output .= "],";
	    } else {
	      # remove the [
	      chop($output);
	      $output .= "null ";
	    }
	    chop($output);
	    $output .= "},\n";
	  }
	}
	# remove last comma and carriage return 
	chop($output);
	chop($output);
	$output .= "]";
	
	# replace all pairs single quotes with one quote:
	$output =~ s/''/"/g;
	# replace all single quotes with double quotes:
	$output =~ s/'/"/g;
	print $output;
